-- Pierwotny właściciel i twórca kodu: AmaMa
-- Zakaz używania / edycji kodu bez wcześniejszej zgody ze strony właściciela
-- Zakaz wykorzystywania całego lub fragmentów kodu
-- W przypadku złamania powyzszych zasad sprawa zostanie skierowana do sądu pod zarzutem: Naruszenia majątkowych praw autorskich
-- Pierwotnie pełne prawo do używania tego kodu posiada tylko AmaMa
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
loadstring(exports.dgs:dgsImportFunction())()
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )
local cmtaBAR=false

local ped = createPed(194,-1986.46802, 85.04347, 27.67986,1)
setElementFrozen(ped,true)

playerDialogue={}
playerBlockDialogue={}

dialogsBarmann ={

    [0] = {
        barman = "Dziękujemy za wizytę w naszym barze ";
        player = "Do widzenia";
    };
    [1] = {
        barman = "Dzień dobry, co podać?";
        player = "Dzień dobry";
    };
    [2] = {
        barman = "Dostępne mamy piwo, whisky oraz wódkę.";
        player = "Proszę mi przedstawić wasze menu.";
    };
    [3] = {
        barman = "Marihuana? Poczekaj chwilę... coś się znajdzie, ale to nie bedą tanie rzeczy...\n50 pkt za sztukę.";
        player = "Marihuany";
    };
    [4] = {
        barman = "Papierosy? Poczekaj chwilę... To będzie \n20 pkt za sztukę.";
        player = "Papierosów";
    };
    [5] = {
        barman = "Luz, uważaj na psy, lubią kręcić się w tej okolicy.";
        player = "Dzięki ziomuś";
    };

}

addEvent("cmta_bar:showGUI", true)
addEventHandler("cmta_bar:showGUI", resourceRoot, function()
	cmtaBAR = true
    playerDialogue[localPlayer] = 0
    local x,y,z = getElementPosition(localPlayer)
    text = dgsCreate3DText(-1986.46802, 85.04347, 27.67986+1.2,"",tocolor(153,50,204,255),false,0.4,0.4,10)
    text2 = dgsCreate3DText(x,y,z+1.2,"",white,false,0.35,0.35,10)
    dgs3DTextAttachToElement(text,ped,0,0,1)
    dgs3DTextAttachToElement(text2,localPlayer,0,0,1)

    img1 = dgsCreateImage(1300*px,460*py,50*px,50*py,":cmta-diler/images/button1.png",false,_,tocolor(255,255,255,255))
    img2 = dgsCreateImage(1300*px,530*py,50*px,50*py,":cmta-diler/images/button2.png",false,_,tocolor(255,255,255,255))
    img3 = dgsCreateImage(1300*px,600*py,50*px,50*py,":cmta-diler/images/button3.png",false,_,tocolor(255,255,255,255))
    img4 = dgsCreateImage(1300*px,670*py,50*px,50*py,"images/button4.png",false,_,tocolor(255,255,255,255))

	textbutton1 = dgsCreateLabel(1230*px,460*py,50*px,50*py,"Rozpocznij rozmowę",false,false)
    dgsSetFont( textbutton1, font )
    dgsSetProperty(textbutton1,"colorcoded",true)
    dgsSetProperty(textbutton1,"alignment",{"right","center"})

	textbutton2 = dgsCreateLabel(1230*px,530*py,50*px,50*py,"Odejdź",false,false)
    dgsSetFont( textbutton2, font )
    dgsSetProperty(textbutton2,"colorcoded",true)
    dgsSetProperty(textbutton2,"alignment",{"right","center"})

	textbutton3 = dgsCreateLabel(1230*px,600*py,50*px,50*py,"",false,false)
    dgsSetFont( textbutton3, font )
    dgsSetProperty(textbutton3,"colorcoded",true)
    dgsSetProperty(textbutton3,"alignment",{"right","center"})

	textbutton4 = dgsCreateLabel(1230*px,670*py,50*px,50*py,"",false,false)
    dgsSetFont( textbutton4, font )
    dgsSetProperty(textbutton4,"colorcoded",true)
    dgsSetProperty(textbutton4,"alignment",{"right","center"})

    dgsSetVisible(img3,false)
    dgsSetVisible(textbutton3,false)
    dgsSetVisible(img4,false)
    dgsSetVisible(textbutton4,false)
    playerDialogue[localPlayer] = 1
    refreshtext()
    setPedFrozen(localPlayer,true)
    setElementRotation(localPlayer,0,0,180)
    toggleControl("forwards",false)
    toggleControl("backwards",false)
    toggleControl("left",false)
    toggleControl("right",false)
    addEventHandler( "onClientKey", getRootElement(), onkey) 

end)

addEvent("cmta_bar:hideGUI", true)
addEventHandler("cmta_bar:hideGUI", resourceRoot, function()
	cmtaBAR = false
    playerDialogue[localPlayer] = 0
    destroyElement(text)
    destroyElement(text2)
    toggleControl("forwards",true)
    toggleControl("backwards",true)
    toggleControl("left",true)
    toggleControl("right",true)
    removeEventHandler( "onClientKey", getRootElement(), onkey) 

end)

function onkey(button,press)
    if cmtaBAR and press == true then
    if playerBlockDialogue[localPlayer] == 1 then return end
    if playerDialogue[localPlayer] == 1 and button == "2" or playerDialogue[localPlayer] == 2 and button == "4" then
        playerDialogue[localPlayer] = 0
        refreshtext()
        setPedFrozen(localPlayer,false)
        toggleControl("forwards",true)
        toggleControl("backwards",true)
        toggleControl("left",true)
        toggleControl("right",true)
        destroyElement(img1)
        destroyElement(img2)
        destroyElement(img3)
        destroyElement(img4)
        destroyElement(textbutton1)
        destroyElement(textbutton2)
        destroyElement(textbutton3)
        destroyElement(textbutton4)
    end
    if playerDialogue[localPlayer] == 3 and button == "3" or playerDialogue[localPlayer] == 4 and button == "3" then
        playerDialogue[localPlayer] = 5
        refreshtext()
        setPedFrozen(localPlayer,false)
        toggleControl("forwards",true)
        toggleControl("backwards",true)
        toggleControl("left",true)
        toggleControl("right",true)
        destroyElement(img1)
        destroyElement(img2)
        destroyElement(img3)
        destroyElement(img4)
        destroyElement(textbutton1)
        destroyElement(textbutton2)
        destroyElement(textbutton3)
        destroyElement(textbutton4)
    end

    if playerDialogue[localPlayer] == 4 and button == "1" then
        triggerServerEvent("cmta_diler:kup", resourceRoot,"papierosy",1)
        block()
    end
    if playerDialogue[localPlayer] == 4 and button == "2" then
        triggerServerEvent("cmta_diler:kup", resourceRoot,"papierosy",5)
        block()
    end
    if playerDialogue[localPlayer] == 3 and button == "1" then
        triggerServerEvent("cmta_diler:kup", resourceRoot,"marihuana",1)
        block()
    end
    if playerDialogue[localPlayer] == 3 and button == "2" then
        triggerServerEvent("cmta_diler:kup", resourceRoot,"marihuana",5)
        block()
    end
    if playerDialogue[localPlayer] == 2 and button == "1" then
        dgsSetText(textbutton1,"Biorę 1 sztukę")
        dgsSetText(textbutton2,"Biorę 5 sztuk")
        dgsSetText(textbutton3,"Odejdź")
        playerDialogue[localPlayer] = 3
        refreshtext()
    end
    if playerDialogue[localPlayer] == 2 and button == "2" then
        dgsSetText(textbutton1,"Biorę 1 sztukę")
        dgsSetText(textbutton2,"Biorę 5 sztuk")
        dgsSetText(textbutton3,"Odejdź")
        playerDialogue[localPlayer] = 4
        refreshtext()
    end
    if playerDialogue[localPlayer] == 1 and button == "1" then
        dgsSetText(textbutton1,"Piwo")
        dgsSetText(textbutton2,"Whisky")
        dgsSetText(textbutton3,"Wódka")
        dgsSetText(textbutton4,"Odejdź")
        dgsSetVisible(img3,true)
        dgsSetVisible(textbutton3,true)
        dgsSetVisible(img4,true)
        dgsSetVisible(textbutton4,true)
        playerDialogue[localPlayer] = 2
        refreshtext()
    end 
    end
end

function refreshtext()
    dgsSetText(text2,dialogsBarmann[playerDialogue[localPlayer]].player)
    dgsSetText(text,"") 
    playerBlockDialogue[localPlayer] = 1
    setTimer(function()
        if isElement(text) then
        dgsSetText(text,dialogsBarmann[playerDialogue[localPlayer]].barman) 
        playerBlockDialogue[localPlayer] = 0
        end
    end,2000,1)
end

function block()
    playerBlockDialogue[localPlayer] = 1
    setTimer(function()
    playerBlockDialogue[localPlayer] = 0
    end,4500,1)
end